import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-07-20
 * Time: 21:08
 */
public class Test {

    public boolean CheckPermutation(String s1, String s2) {
        int len1 = s1.length();
        int len2 = s2.length();
        if(len1 != len2) return false;
        Map<Character,Integer> map = new HashMap<>();
        for(int i = 0; i < len1; i++) {
            char ch = s1.charAt(i);
            if(map.get(ch) == null) {
                map.put(ch,1);
            }else {
                map.put(ch,map.get(ch) + 1);
            }
        }
        for(int i = 0; i < len2; i++) {
            char ch = s2.charAt(i);
            if(map.get(ch) == null || map.get(ch) == 0) {
                return false;
            }else {
                map.put(ch,map.get(ch) - 1);
            }
        }
        return true;
    }

    public int lastStoneWeight(int[] stones) {
        int len = stones.length;
        while(len != 0) {
            Arrays.sort(stones);
            if(len-2 < 0 || len - 1 < 0) {
                break;
            }
            int x = stones[len-2];
            int y = stones[len-1];
            if(x == y) {
                len -= 2;
            }else {
                stones[len-2] = y - x;
                len--;
            }
        }
        return stones[0];
    }
}
